﻿@using System.Net.Sockets;
@using BootstrapBlazorApp.Shared.Data.SYS;
@using WebApiClientCore;
@using WebApiClientCore.Exceptions;

@code {

    [Inject] MessageService Message{ get; set; }
    static MessageService MessageService { get; set; }

    public static async Task<bool> ExecuteApiAsync(Func<ITask<ResponseModel>> action) 
    {
        string msg = "";
        try
        {
            var result = await action();
            if (!result.succeeded)
                await MessageService.Show(new MessageOption()
                    {
                        Color = Color.Danger,
                        Content = result.errors
                    });
            return result.succeeded;
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiInvalidConfigException configException)
        {
            // 请求配置异常
            msg = configException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiResponseStatusException statusException)
        {
            // 响应状态码异常
            msg = statusException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiException apiException)
        {
            // 抽象的api异常
            msg = apiException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is SocketException socketException)
        {
            // socket连接层异常
            msg = socketException.Message;
        }
        catch (HttpRequestException ex)
        {
            // 请求异常
            msg = ex.Message;
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        await MessageService.Show(new MessageOption()
            {
                Color = Color.Danger,
                Content = msg
            });
        return false;
    }

    public static async Task<T> ExecuteApiAsync<T>(Func<ITask<ResponseModel<T>>> action) where T : new()
    {
        string msg = "";
        try
        {
            var result = await action();
            if (result.succeeded)
                return result.data;
            else
                await MessageService.Show(new MessageOption()
                    {
                        Color = Color.Danger,
                        Content = result.errors
                    });
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiInvalidConfigException configException)
        {
            // 请求配置异常
            msg = configException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiResponseStatusException statusException)
        {
            // 响应状态码异常
            msg = statusException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is ApiException apiException)
        {
            // 抽象的api异常
            msg = apiException.Message;
        }
        catch (HttpRequestException ex) when (ex.InnerException is SocketException socketException)
        {
            // socket连接层异常
            msg = socketException.Message;
        }
        catch (HttpRequestException ex)
        {
            // 请求异常
            msg = ex.Message;
        }
        catch (Exception ex)
        {
            msg = ex.Message;
        }
        await MessageService.Show(new MessageOption()
            {
                Color = Color.Danger,
                Content = msg
            });
        return new T();
    }

    /// <summary>
    /// OnInitializedAsync 方法
    /// </summary>
    /// <returns></returns>
    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();

        MessageService = Message;
    }
}
